package com.kaisu.blog.infrastructure.user.convert.decorated;

import com.kaisu.blog.domain.file.repository.FileRepository;
import com.kaisu.blog.domain.user.User;
import com.kaisu.blog.infrastructure.user.UserPO;
import com.kaisu.blog.infrastructure.user.convert.UserConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author kaisu
 */
@Component
public abstract class UserConvertDecorated implements UserConvert {
    @Autowired
    private UserConvert convert;
    @Autowired
    private FileRepository fileRepository;

    @Override
    public User toDomain(UserPO po) {
        User domain = convert.toDomain(po);
        if (Objects.nonNull(po.getAvatarId())) {
            domain.setAvatar(fileRepository.findById(po.getAvatarId()));
        }
        return domain;
    }
}
